// 获取未中奖金额
import {
	isNewUser
} from '../utils/common.js'
export function getOffMony() {
	// 概率 
	const moneys = [20, 50, 100, 500, 1000, 10000, 50000, 100000, 500000, 1000000]
	const rate = getRandomNum(100);
	let level = 0;
	if (rate < 20) {
		level = 0
	} else if (rate < 30) {
		level = 1
	} else if (rate < 40) {
		level = 2
	} else if (rate < 50) {
		level = 3
	} else if (rate < 60) {
		level = 4
	} else if (rate < 70) {
		level = 5
	} else if (rate < 80) {
		level = 6
	} else if (rate < 90) {
		level = 7
	} else if (rate < 95) {
		level = 8
	} else if (rate <= 100) {
		level = 9
	} else {
		level = 0
	}

	return moneys[level]
}


// 获取中奖金额
export function getOnNewUserMoney() {
	// 概率 
	const rate = getRandomNum(1000000);
	const moneys = [20, 50, 100, 500, 1000, 10000, 50000, 100000, 500000, 1000000]
	let level = 0;
	if (rate < 600000) { // 20
		level = 2
	} else if (rate < 700000) { // 50
		level = 3
	} else if (rate < 800000) { // 100
		level = 4
	} else if (rate < 900000) { // 500
		level = 5
	} else if (rate < 990000) { // 1000
		level = 4
	} else if (rate < 999000) { // 10000
		level = 5
	} else if (rate < 999900) { // 50000
		level = 6
	} else if (rate < 999990) { // 100000
		level = 7
	} else if (rate < 999998) { // 500000
		level = 8
	} else if (rate <= 1000000) { //1000000
		level = 9
	} else {
		level = 0
	}
	const money = moneys[level]
	if (!money || money == 0 || money == null || money == undefined || money > 1000000) {
		console.log("获取中奖金额异常2------------------\n" + money + JSON.stringify(rates) + "," + JSON.stringify(moneys));
		return 100;
	} else {
		console.log("getOnMony222:"+money);
		return money;
	}
}

// 获取中奖金额
export function getOnMony(rates, moneys) {
	try{
		// 服务端数据有问题 采用本地策略
		if (rates.length != moneys.length || !rates || !moneys || rates.length == 0 || moneys.length == 0 ) {
			return getOnNewUserMoney();
		}
		// 概率 
		const rate = getRandomNum(rates[rates.length - 1]);
		let level = 0;
		for (var i = 0; i < rates.length; i++) {
			if (rate < rates[i]) {
				level = i;
				break;
			}
		}
		const money = moneys[level]
		if (!money || money == 0 || money == null || money == undefined||money>moneys[moneys.length - 1]) {
			console.log("获取中奖金额异常1------------------\n" + money + JSON.stringify(rates) + "," + JSON.stringify(moneys));
			return 100;
		} else {
			console.log("getOnMony111rate:"+rate+",level:"+level+",money:"+money);
			return money;
		}
	}catch(e){
		return 100;
	}
}

// 获取中奖金额
export function getOnMonyAndLevel(rates, moneys) {
	try{
		// 服务端数据有问题 采用本地策略
		if (rates.length != moneys.length || !rates || !moneys || rates.length == 0 || moneys.length == 0 ) {
			return {level:1,money:100};
		}
		// 概率 
		const rate = getRandomNum(rates[rates.length - 1]);
		let level = 0;
		for (var i = 0; i < rates.length; i++) {
			if (rate < rates[i]) {
				level = i;
				break;
			}
		}
		const money = moneys[level]
		if (!money || money == 0 || money == null || money == undefined||money>moneys[moneys.length - 1]) {
			console.log("获取中奖金额异常1------------------\n" + money + JSON.stringify(rates) + "," + JSON.stringify(moneys));
			return {level:1,money:100};
		} else {
			console.log("getOnMony111rate:"+rate+",level:"+level+",money:"+money);
			return {level:level+1,money:money};
		}
	}catch(e){
		return {level:1,money:100};
	}
}

export function getRandomNum(num) {
	return Math.floor(Math.random() * num) + 1;
}

export function getRandomNumExcepts(num, excepts) {
	let rand = getRandomNum(num);
	// 递归
	if (excepts.includes(rand)) {
		rand = getRandomNumExcepts(num, excepts)
		return rand
	} else {
		return rand;
	}
}

export function getPinyinWithNum(num) {
	const numToZh = ["LING", "YI", "ER", "SAN", "SI", "WU", "LIU", "QI", "BA", "JIU", "SHI"]
	if (num < 10) {
		return numToZh[0] + numToZh[num];
	} else {
		const shi = parseInt(num / 10);
		const ge = parseInt(num % 10);
		return numToZh[shi] + numToZh[ge];
	}
}

//数字前面加0
export function padZero(number, length) {
	return String(number).padStart(length, '0');
}